﻿using System;
using NUnit.Framework;
using SmartPeople.Framework.SecuritySpace;

namespace SmartPeople.Framework.SecuritySpace.Provider.Tests
{
    [TestFixture]
    public class SecuritySessionTests
    {
        [Test]
        public void CreateSession()
        {
            //create document
            //create roles
            //create users
            //assign users to roles
            //give user permission
            //authenticate user
            //create session for user

            ISecuritySpace securitySpace = TestsHelper.GetSecuritySpace("TA");
            securitySpace.Clear();
            ISecurityRole securityRole =
                securitySpace.Roles.CreateRole("Normal", "Normal User", "Normal user for normal operations");
            Assert.AreEqual(1, securitySpace.Roles.Count);
            ISecurityUser securityUser = securitySpace.Users.CreateUser("user1", "password123", "user1@acme.com");
            Assert.AreEqual(1, securitySpace.Users.Count);
            securityUser.PasswordQuestion = "What is you fathers middle name?";
            securityUser.IsApproved = true;
            securitySpace.Users.UpdateUser(securityUser);


            string[] usernames = {"user1"};
            string[] rolenames = {"Normal"};
            securitySpace.Roles.AddUsersToRoles(usernames, rolenames);
            Assert.AreEqual(true, securitySpace.Roles.IsUserInRole("user1", "Normal"));
            ISecuritySecuredType commonFolder = securitySpace.SecuredTypes.CreateType("Common");
            securitySpace.SecuredDocuments.SetDefaultPathType(commonFolder);
            ISecuritySecuredType commonFile = securitySpace.SecuredTypes.CreateType("Gender");
            securitySpace.SecuredDocuments.SetDefaultDocumentType(commonFile);

            commonFolder.Actions.CreateAction("List");
            commonFolder.Actions.CreateAction("Read");
            commonFolder.Actions.CreateAction("Create");
            commonFolder.Actions.CreateAction("Modify");
            commonFolder.Actions.CreateAction("Delete");
            string commomPath = "/TA/Common";
            securitySpace.SecuredDocuments.CreatePath("commomPath");
            bool authorized;
            authorized = securitySpace.IsUserGranted("user1", commomPath, "List");

            IToken authtoken =
                securitySpace.Users.Authenticate("user1", "password123", "128.0.0.1", DateTime.Now, 30, DateTime.Now);
            Assert.IsInstanceOfType(typeof (IToken), authtoken);
            Assert.IsNotNull(authtoken);

            securitySpace.Clear();
        }
    }
}